#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>

#include "Trajectory.h"

Trajectory::Trajectory(void)
{
	redCol = 0;
	greenCol = 0;
	blueCol = 0;

	this->addStartVertex();
}

void Trajectory::addStartVertex()
{
	// First vertex at origin
	Vec2 firstVertex(0.0,0.0);
	_vertices.push_back(firstVertex);
}

Trajectory::~Trajectory(void)
{
}


void Trajectory::render() const
{
	glColor3f(redCol, greenCol, blueCol);
	glLineWidth(3.0f);
	glBegin(GL_LINE_STRIP);

	

	for( unsigned int i = 0; i < nVertices() ; ++i)
	{
		glVertex2f( _vertices[i].x, _vertices[i].y );
	}
	//glVertex2f(_nextVertex.x, _nextVertex.y);
	glEnd();
}

void Trajectory::updateNextVertex(const Vec2& vertex)
{
	_nextVertex = vertex;
}

void Trajectory::addNextVertex()
{
	// Add the vertex to the end of the vector
	if(_vertices.empty() || _nextVertex.y>_vertices.back().y)
		_vertices.push_back( _nextVertex );

	std::cout << _nextVertex.x << "," << _nextVertex.y << std::endl;
	
}

const Vec2& Trajectory::vertex(unsigned int num) const
{
	return _vertices[num];
}

unsigned int Trajectory::nVertices() const
{
	return (unsigned int)_vertices.size();
}

// Stores the Shape
void Trajectory::save( std::ostream& output )
{
	// Write the number of points
	output << _vertices.size() << std::endl;
	for(unsigned int i = 0; i < _vertices.size(); ++i)
	{
		output << _vertices[i].x << " " << _vertices[i].y << std::endl;
	}
}

// Loads a Shape
void Trajectory::load( std::istream& input )
{
	// Clear the previous vertices (if any)
	_vertices.clear();

	unsigned int total;
	input >> total;

	for(unsigned int i = 0; i < total; ++i)
	{
		float x, y;
		input >> x >> y;
		_vertices.push_back( Vec2(x, y) );
	}
}

void Trajectory::deleteAll()
{
	_vertices.clear();
	this->addStartVertex();
}

void Trajectory::setColor(float red, float green, float blue)
{
	redCol = red;
	greenCol = green;
	blueCol = blue;
}

void Trajectory::deleteLastVertex()
{
	// Remove the last vertex if its not the one at the origin
	if(_vertices.size() >1 )
		_vertices.pop_back();
}